<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class collegian_activity_model  extends CI_Model {

    public function __construct() {
        error_reporting(0);
        parent::__construct();
    }
    
    /* เรียกดูข้อมูลกิจกรรม */
    public function get_activity_detail_by_id( ){

    }
    
    public function get_collegian_in_activity($act_id  ){
      
                    $this->db->select("*,MID(collegians_of_activity.code_active,1,10) as  codestring",false)->from("activity_events")
                            ->join("activity_groups_relation","activity_groups_relation.ref_act_id = activity_events.act_id","inner")
                            ->join("group_major_relation","group_major_relation.ref_activity_groups_relation_id = activity_groups_relation.activity_groups_relation_id","inner")
                            ->join("collegians_of_activity" , "collegians_of_activity.ref_group_major_relation_id = group_major_relation.group_major_relation_id","inner")
                            ->join("majors" , "majors.major_id = group_major_relation.ref_major_id","inner")
                            ->join("collegian" , "collegian.collegian_id = collegians_of_activity.ref_collegian_id","inner")
/*
 * 
 * 
 *                             ->where("allow_activity","Y")
                            ->where("is_active","Y")
 */
                            ->where("activity_events.act_id", $act_id)
                            ->order_by("group_major_relation.ref_major_id","DESC");
        return $this->db->get();
    }
    
    /* กิจกรรมบังคับ */
    public function get_report_activity_y($collegian_id ){
      
                    $this->db->select("*")->from("activity_events")
                            ->join("activity_groups_relation","activity_groups_relation.ref_act_id = activity_events.act_id","inner")
                            ->join("group_major_relation","group_major_relation.ref_activity_groups_relation_id = activity_groups_relation.activity_groups_relation_id","inner")
                            ->join("collegians_of_activity" , "collegians_of_activity.ref_group_major_relation_id = group_major_relation.group_major_relation_id","inner")
                            ->where("ref_collegian_id",$collegian_id)
                            ->where("allow_activity","N")
                            ->where("act_type","YES")
                            ->order_by("activity_events.act_id","DESC");

        return $this->db->get();
    }
    
    /* ข้อมูลการเข้าร่วมกิจกรรม */
    public function get_report_activity_n($collegian_id ){
      
                    $this->db->select("*")->from("activity_events")
                            ->join("activity_groups_relation","activity_groups_relation.ref_act_id = activity_events.act_id","inner")
                            ->join("group_major_relation","group_major_relation.ref_activity_groups_relation_id = activity_groups_relation.activity_groups_relation_id","inner")
                            ->join("collegians_of_activity" , "collegians_of_activity.ref_group_major_relation_id = group_major_relation.group_major_relation_id","inner")
                            ->where("ref_collegian_id",$collegian_id)
                            ->where("allow_activity","Y")
                            ->order_by("activity_events.act_id","DESC");

        return $this->db->get();
    }
    
    
    /* ตรวจสอบนักศึกษาว่าเข้าร่วมกิจกรรมที่ระบุ หรือไม่ */
    public function check_collegian_in_activity( $collegian_id , $act_id , $activity_type  ){
        if($activity_type == "N"){
                    $this->db->select("*")->from("activity_events")
                            ->join("activity_groups_relation","activity_groups_relation.ref_act_id = activity_events.act_id","inner")
                            ->join("group_major_relation","group_major_relation.ref_activity_groups_relation_id = activity_groups_relation.activity_groups_relation_id","inner")
                            ->join("collegians_of_activity" , "collegians_of_activity.ref_group_major_relation_id = group_major_relation.group_major_relation_id","inner")
                            ->where("ref_collegian_id",$collegian_id)
                            ->where("activity_events.act_id",$act_id)
                            ->where("act_type","NO")
                            ->limit(1);
        }else{
                    $this->db->select("*")->from("activity_events")
                            ->join("activity_groups_relation","activity_groups_relation.ref_act_id = activity_events.act_id","inner")
                            ->join("group_major_relation","group_major_relation.ref_activity_groups_relation_id = activity_groups_relation.activity_groups_relation_id","inner")
                            ->join("collegians_of_activity" , "collegians_of_activity.ref_group_major_relation_id = group_major_relation.group_major_relation_id","inner")
                            ->where("ref_collegian_id",$collegian_id)
                            ->where("activity_events.act_id",$act_id)
                            ->where("act_type","YES")
                            ->limit(1);
        }
        return $this->db->get();
    }
    
    
    
    /* เรียกดูข้อมูลกิจกรรม จากประเภทกิจกรรม และรหัสนักศึกษา  Y = กิจกรรมบังคับ , N = กิจกรรมเลือก */
    public function get_collegian_activity($collegian_id , $activity_type = "Y"){
        /* กิจกรรมบังคับ */
        if($activity_type == "Y"){
                    $this->db->select("*")->from("activity_events")
                            ->join("activity_groups_relation","activity_groups_relation.ref_act_id = activity_events.act_id","inner")
                            ->join("group_major_relation","group_major_relation.ref_activity_groups_relation_id = activity_groups_relation.activity_groups_relation_id")
                            ->join("collegians_of_activity" , "collegians_of_activity.ref_group_major_relation_id = group_major_relation.group_major_relation_id")
                            ->where("ref_collegian_id",$collegian_id)
                            ->where("act_type","YES")
                            ->where("status","OPEN")
                            ->group_by("activity_events.act_id")
                            ->order_by("activity_events.act_start_date","DESC");
        }else{
                    $this->db->select("*")->from("activity_events")
                            ->where("act_type","NO")
                            ->where("status","OPEN")
                            ->order_by("activity_events.act_start_date","DESC");
        }
        return $this->db->get();
        
    }
}
?>
